package com.wpml.netty.test;

import java.lang.reflect.Field;

/**
 * @Description
 * @Date 2021-08-12
 * @Author wangpei
 **/
public class ReflectDemo {

    public static void main(String[] args) throws Exception{
        Class<Student> clazz = Student.class;

        Student student = clazz.newInstance();
        System.out.println(student);

        Field staticName = clazz.getDeclaredField("staticName");
//        System.out.println("原始值： " + staticName.get);
        staticName.set(null, "staticName_002");

        Field name = clazz.getDeclaredField("name");
        name.setAccessible(true);
        name.set(student, "ooxx");
        System.out.println(student);


       /* Field[] declaredFields = clazz.getDeclaredFields();
        // 获取public修饰的属性变量
//        Field[] declaredFields = clazz.getFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField.getName());
        }*/
    }
}

class Student {
    private String name = "xxoo";
    public static String staticName = "staticName_001";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static String getStaticName() {
        return staticName;
    }

    public static void setStaticName(String staticName) {
        Student.staticName = staticName;
    }

    @Override
    public String toString() {
        return "name = " + name + ", staticName = " + staticName;
    }
}
